package com.project.lx.gettxtfilename.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

import com.project.lx.gettxtfilename.R;

/**
 * @author: Meteor
 * @description: 圆角图片
 * @version:
 * @date: 2017/2/26 0026 10:13
 * @company: jijiaxuan.com
 * @email: lx802315@163.com
 */
public class RoundImageView extends ImageView {

    private Paint paint;

    private int roundPx;        //圆角的角度

    private boolean isCircle;       //是否为圆形

    private int defroundPx = 15;        //角度默认值

    private boolean defisCircle = false;        //默认为圆角

    public int getRoundPx() {
        return roundPx;
    }

    public void setRoundPx(int roundPx) {
        this.roundPx = roundPx;
    }

    public boolean isCircle() {
        return isCircle;
    }

    public void setCircle(boolean circle) {
        isCircle = circle;
    }

    public RoundImageView(Context context) {
        this(context, null);
    }

    public RoundImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setAttrs(context, attrs);
        paint = new Paint();
    }

    /**
     * 设置xml文件中配置的属性
     *
     * @param context
     * @param attrs
     */
    public void setAttrs(Context context, AttributeSet attrs) {

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundImageView);
        if (ta != null) {
            roundPx = ta.getInteger(R.styleable.RoundImageView_roundPx1, defroundPx);
            isCircle = ta.getBoolean(R.styleable.RoundImageView_isCircle1, defisCircle);
            ta.recycle();
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {

        Drawable drawable = getDrawable();      //获取该imageView设置的src图片

        if (drawable != null) {

            Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
            Bitmap b;

            if (isCircle) {
                b = getCircleBitmap(bitmap);
            } else {
                b = getRoundBitmap(bitmap, roundPx);
            }

            final Rect rectSrc = new Rect(0, 0, b.getWidth(), b.getHeight());
            final Rect rectDest = new Rect(0, 0, getWidth(), getHeight());

            paint.reset();
            canvas.drawBitmap(b, rectSrc, rectDest, paint);

        } else {
            super.onDraw(canvas);
        }

    }

    /**
     * 获取圆形图片方法
     *
     * @param source
     * @return Bitmap
     * @author caizhiming
     */
    private Bitmap getCircleBitmap(Bitmap source) {

        int length = source.getWidth() < source.getHeight() ? source.getWidth() : source.getHeight();
        paint.setAntiAlias(true);
        Bitmap target = Bitmap.createBitmap(length, length, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(target);
        canvas.drawCircle(length / 2, length / 2, length / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(source, 0, 0, paint);
        return target;

    }

    /**
     * 获取圆角矩形图片方法
     *
     * @param bitmap
     * @param roundPx,一般设置成15
     * @return Bitmap
     * @author caizhiming
     */
    private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;

        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        int x = bitmap.getWidth();

        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;

    }
}
